/*
Copyright 2005-2014 CP Fricano - All Rights Reserved

This software and source code is licensed under the Apache
License, Version 2.0. You may not use this software or source
code except in compliance with the License.  You may obtain a
copy of the License at: http://www.apache.org/licenses/LICENSE-2.0.
Use and distributed under this License is on an "as is" basis,
without expressed or implied warranties or conditions of any kind.
See the License for the specific language governing permissions and
limitations.
 */

package com.fricano.java42.instructional.example.simulation;

public class h {

    public static void main(final String args[]) throws Exception {

        {
            final int maxLoops = 100;
            double iterationsAvg = 0;
            double millisecAvg = 0;
            for (int x = 0; x != maxLoops; x++) {
                {
                    final long startMS = System.currentTimeMillis();
                    long nowMS = System.currentTimeMillis();
                    long iterations = 0;
                    while ((nowMS = System.currentTimeMillis()) == startMS) {
                        iterations++;
                    }
                    iterationsAvg += iterations;
                    millisecAvg += (nowMS - startMS);
                }
            }
            final double avgR = millisecAvg / maxLoops;
            final double avgI = iterationsAvg / maxLoops;
            // System.out.println("   NS/interation  =" + ((millisecAvg * 1000000) / avgI) / maxLoops);
            // System.out.println("Avegage iterations=" + avgI);
            System.out.println("System.currentTimeMillis() resolution is " + avgR + " milliseconds");
        }

        {
            final int maxLoops = 1000000;
            double iterationsAvg = 0;
            double nanosecAvg = 0;
            for (int x = 0; x != maxLoops; x++) {
                {
                    final long startNS = System.nanoTime();
                    long nowNS = System.nanoTime();
                    long iterations = 0;
                    while ((nowNS = System.nanoTime()) == startNS) {
                        iterations++;
                    }
                    iterationsAvg += iterations;
                    nanosecAvg += (nowNS - startNS);
                }
            }
            final double avgR = nanosecAvg / maxLoops;
            final double avgI = iterationsAvg / maxLoops;
            // System.out.println("   NS/interation  =" + ((nanosecAvg) / avgI) / maxLoops);
            // System.out.println("Avegage iterations=" + avgI);
            System.out.println("System.nanoTime() resolution is " + avgR + " nanoseconds");
            System.exit(0);
        }

        final int fps = 600; // target frames per second
        final float simulationCaptureNowMS = .40f; // MS to capture a frame
        final float simulationSaveNowNowMS = .40f; // MS to save a frame

        final long simulationCaptureNowNS = (long) (simulationCaptureNowMS * 1000000.0f);
        final long simulationSaveNowNowNS = (long) (simulationSaveNowNowMS * 1000000.0f);
        final long windowNS = (1000 * 1000000) / fps;
        final long movieDurationSEC = 2;
        final long dropDeadTimeMS = System.currentTimeMillis() + (1000 * movieDurationSEC);
        while (System.currentTimeMillis() < dropDeadTimeMS) {
            final long startNS = System.nanoTime();
            actionSimulator(simulationCaptureNowNS);
            actionSimulator(simulationSaveNowNowNS);
            final long endNS = System.nanoTime();
            final long sleepNS = windowNS - (endNS - startNS);
            // System.out.println(sleepNS);
            if (sleepNS < 0) {
                System.out.println("Data loss. Try again.");
                System.exit(0);
            }
            actionSimulator(sleepNS);
        }
        System.out.println("No data loss at " + fps + "fps with interframe latency of " + (simulationCaptureNowMS + simulationSaveNowNowMS) + "ms");
    }

    private static void actionSimulator(final long ns) throws Exception {
        final long d = System.nanoTime() + ns;
        while (System.nanoTime() < d)
            Thread.yield();
    }
}
